package ai.xiaodao.config;

import ai.xiaodao.tool.ConfigUtil;
import com.futu.openapi.pb.QotCommon;
import com.futu.openapi.pb.TrdCommon;
import org.apache.log4j.Logger;

import java.util.Properties;

import static java.lang.System.exit;

public class Security {
    static Logger log = Logger.getLogger(Security.class.getName());
    private static Properties prop = new Properties();
    int status;

    public static Properties getProp() {
        return prop;
    }

    public static int getTrdQty() {
        return trdQty;
    }

    static int market;
    static String code;
    static double buyTriggerPrice;
    static double saleTriggerPrice;
    static int trdQty;
    static double buyPrice;
    static double salePrice;
    static int trdEnv;

    public static int getTrdEnv() {
        return trdEnv;
    }

    public static void setTrdEnv(int trdEnv) {
        Security.trdEnv = trdEnv;
    }

    public static double getBuyTriggerPrice() {
        return buyTriggerPrice;
    }

    public static double getSaleTriggerPrice() {
        return saleTriggerPrice;
    }

    public static double getBuyPrice() {
        return buyPrice;
    }

    public static double getSalePrice() {
        return salePrice;
    }

    public static int getMarket() {
        return market;
    }

    public static String getCode() {
        return code;
    }

    public static void getIns(String conf) {
        //check the input config file path
        if (null != conf && conf.length() > 0) {
            prop = ConfigUtil.readProps(conf);
            code = prop.getProperty(Const.SEC_CODE);
            market = getMarket(prop.getProperty(Const.SEC_MARKET));
            buyTriggerPrice = Double.valueOf(prop.getProperty(Const.SEC_BTRD_TRIGGER_PRICE));
            saleTriggerPrice = Double.valueOf(prop.getProperty(Const.SEC_STRD_TRIGGER_PRICE));
            trdQty = Integer.valueOf(prop.getProperty(Const.SEC_QTY));
            salePrice = Double.valueOf(prop.getProperty(Const.SEC_STRD_PRICE));
            buyPrice = Double.valueOf(prop.getProperty(Const.SEC_BTRD_PRICE));
            trdEnv = Integer.valueOf(prop.getProperty(Const.TRD_ENV));

        } else {
            log.error("The config path in correct please check");
            exit(0);
        }
    }

    public int getStatus() {
        return status;
    }

    public Security setStatus(int status) {
        this.status = status;
        return this;
    }

    /**
     * get the code from the security config file and transfer to internal code
     *
     * @param key
     * @return
     */

    static int getMarket(String key) {
        switch (key) {
            case "hk":
                return QotCommon.TradeDateMarket.TradeDateMarket_HK_VALUE;
            case "us":
                return QotCommon.TradeDateMarket.TradeDateMarket_US_VALUE;
            case "sh":
                return QotCommon.TradeDateMarket.TradeDateMarket_CN_VALUE;
        }
        return 0;
    }

    static int parseTrdSide(String key) {
        switch (key) {
            case "buy":
                return TrdCommon.TrdSide.TrdSide_Buy_VALUE;
            case "sale":
                return TrdCommon.TrdSide.TrdSide_Sell_VALUE;
        }
        return 0;
    }
}
